import { EnumMicroService } from '../types/types';
import { configuration } from '../config/config';
import { promises as fs } from 'fs';
import path from 'path';

async function createLogPath(systemType: EnumMicroService) {
  const logPath = configuration.logPathPrefix + '\\' + systemType;
  try {
    await checkOrCreatePath(logPath);
  } catch (e) {
    console.log(`创建日志路径的时候出现错误： ${e.message}`);
  }
}

async function checkOrCreatePath(filePath) {
  try {
    await fs.access(filePath);
  } catch (e) {
    console.log(`文件路径不存在，创建路径：${filePath}`);
    await createPath(filePath);
  }
}

async function createPath(filePath) {
  const dir = path.dirname(filePath);
  try {
    await fs.mkdir(dir, { recursive: true });
  } catch (e) {
    console.log(`创建filePath，error:${e.message}`);
  }
}
